package com.yuexi.domain.util;

import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author jie
 * @date 2022/12/14 -15:57
 */
public class StringConvertUtil {

    /**
     * 转换成list，用逗号分隔
     *
     * @param str str
     * @return list
     */
    public static List<String> toList(String str) {
        if (!StringUtils.hasLength(str)) {
            return new ArrayList<>();
        }
        return Arrays.asList(str.split(","));
    }

    /**
     * 将list通过逗号拼接成一个字符串
     *
     * @param list list
     * @return 拼接后的字符串
     */
    public static String toString(List<String> list) {
        if (CollectionUtils.isEmpty(list)) {
            return "";
        }
        return String.join(",", list);
    }

    public static void main(String[] args) {
        List<String> list = toList("a,b,c");
        list.forEach(System.out::println);

        String string = toString(list);
        System.out.println(string);
    }
}
